package com.xy.view.test;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.xy.debug.LogUtils;

/**
 * Created by John on 2016/11/28.
 */

public class CustumWebView extends WebView {
    public CustumWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    public CustumWebView(Context context) {
        super(context);
        init(context);
    }

    public CustumWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        createWebView();
    }

    private void createWebView() {
        final WebSettings settings = getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(true); //支持缩放
        settings.setDisplayZoomControls(true); //适应屏幕
        settings.setLoadWithOverviewMode(true); //缩放至屏幕大小

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //在安卓4.4以上，在加载image标签时，假如有多个image标签时相同的src时，会只加载同一个，所以就让它自动加载。
            settings.setLoadsImagesAutomatically(true);
        } else {
            settings.setLoadsImagesAutomatically(false);
        }
        setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LogUtils.i("url:" + url);
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (!settings.getLoadsImagesAutomatically()) {
                    settings.setLoadsImagesAutomatically(true);
                }
            }


        });
    }

}
